package com.ebraga.trackerclub.android.util.webservice;

import java.io.InputStream;
import java.util.Collection;

import org.apache.http.HttpStatus;

import com.ebraga.trackerclub.android.exception.BackofficeConnectionException;
import com.ebraga.trackerclub.android.exception.BackofficeUtilException;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;

/**
 * Wrapper for HTTPS Client to avoid further engagement and make the API more simple. <br/>
 * Provides basic methods to perform HTTPS calls.
 *
 * @param <T> the expected type to be converted into JSON String on Post call. 
 * @see HTTPSClientWrapper#doPost(String, E)
 */
public class HTTPSClientWrapper<E> {
	
	private final Client client;
	
	/**
	 * Returns an instance of HTTPSClientWrapper.
	 * 
	 * @param trustStoreInputStream1 the trust store input stream. 
	 * @param trustStorePassword1 trust store password.
	 * @param trustStoreInputStream2 the trust store input stream. 
	 * @param trustStorePassword2 trust store password. 
	 * @throws BackofficeUtilException if any error occurs.
	 */
	public HTTPSClientWrapper(InputStream trustStoreInputStream1, String trustStorePassword1, InputStream trustStoreInputStream2, String trustStorePassword2) throws BackofficeUtilException {
		this.client = HTTPSClientUtil.getClient(trustStoreInputStream1, trustStorePassword1, trustStoreInputStream2, trustStorePassword2);
	}
	
	/**
	 * Returns an instance of HTTPSClientWrapper.
	 * 
	 * @param trustStoreInputStream1 the trust store input stream. 
	 * @param trustStorePassword1 trust store password.
	 * @param trustStoreInputStream2 the trust store input stream. 
	 * @param trustStorePassword2 trust store password.
	 * @param user
	 * @param password
	 * @throws BackofficeUtilException if any error occurs.
	 */
	public HTTPSClientWrapper(InputStream trustStoreInputStream1, String trustStorePassword1, InputStream trustStoreInputStream2, String trustStorePassword2, HTTPBasicAuthFilter httpBasicAuthFilter) throws BackofficeUtilException {
		this.client = HTTPSClientUtil.getClient(trustStoreInputStream1, trustStorePassword1, trustStoreInputStream2, trustStorePassword2, httpBasicAuthFilter);
	}
	
	/**
	 * Returns the contents of the POST done for the given URL sending the postContent as JSON if the response status is {@link HttpStatus.SC_CREATED}.
	 * 
	 * @param url the target URL.
	 * @param e the content to be posted.
	 * @return the contents of the POST done through the Client offered for the given URL as JSON.
	 * @throws BackofficeConnectionException if a connection error occurs.
	 * @throws BackofficeUtilException if any error occurs.
	 */
	public String doPost(String url, Collection<E> e) throws BackofficeUtilException, BackofficeConnectionException {
		String jsonString = JSONUtil.toJSON(e);
		return HTTPSClientUtil.doPost(client, url, jsonString);
	}
	
	/**
	 * Returns the contents of the POST done for the given URL sending the postContent as JSON if the response status is {@link HttpStatus.SC_CREATED}.
	 * 
	 * @param url the target URL.
	 * @param postContent the content to be posted.
	 * @return the contents of the POST done through the Client offered for the given URL as JSON.
	 * @throws BackofficeConnectionException if a connection error occurs.
	 * @throws BackofficeUtilException if any error occurs.
	 */
	public String doPost(String url, E e) throws BackofficeUtilException, BackofficeConnectionException {
		String jsonString = JSONUtil.toJSON(e);
		return HTTPSClientUtil.doPost(client, url, jsonString);
	}
	

	/**
	 * Returns the contents of the PUT done for the given URL sending the putContent as JSON if the response status is {@link HttpStatus.SC_CREATED}.
	 * 
	 * @param url the target URL.
	 * @param e the content to be posted.
	 * @return the contents of the PUT done through the Client offered for the given URL as JSON.
	 * @throws BackofficeConnectionException if a connection error occurs.
	 * @throws BackofficeUtilException if any error occurs.
	 */
	public String doPut(String url, Collection<E> e) throws BackofficeUtilException, BackofficeConnectionException {
		String jsonString = JSONUtil.toJSON(e);
		return HTTPSClientUtil.doPut(client, url, jsonString);
	}
	
	/**
	 * Returns the contents of the PUT done for the given URL sending the putContent as JSON if the response status is {@link HttpStatus.SC_CREATED}.
	 * 
	 * @param url the target URL.
	 * @param putContent the content to be posted.
	 * @return the contents of the PUT done through the Client offered for the given URL as JSON.
	 * @throws BackofficeConnectionException if a connection error occurs.
	 * @throws BackofficeUtilException if any error occurs.
	 */
	public String doPut(String url, E e) throws BackofficeUtilException, BackofficeConnectionException {
		String jsonString = JSONUtil.toJSON(e);
		return HTTPSClientUtil.doPut(client, url, jsonString);
	}	
	
	/**
	 * Returns the contents of the GET done for the given URL if the response status is {@link HttpStatus.SC_OK}.
	 * 
	 * @param url the target URL.
	 * @return the contents of the GET done through the Client offered for the given URL as JSON.
	 * @throws BackofficeConnectionException if a connection error occurs.
	 * @throws BackofficeUtilException if any error occurs.
	 */
	public String doGet(String url) throws BackofficeUtilException, BackofficeConnectionException {
		return HTTPSClientUtil.doGet(client, url);
	}
	

}
